﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchEdu.Framework.DataModel;


namespace SchEduApp
{
    public partial class OrderForm : Form
    {
        DataGridHelper dataGridHelper = new DataGridHelper();

        public OrderForm()
        {
            InitializeComponent();
            this.orderBindingSource.DataSource = SchEduAppContext.SchEduData.SchEduDataSet;
            this.workCenterBindingSource.DataSource = SchEduAppContext.SchEduData.SchEduDataSet;
        }

        private void orderGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            dataGridHelper.Validate(sender, e);
        }

        private void orderGridView_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            dataGridHelper.ResetError(sender, e);
        }

        private void orderGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            dataGridHelper.ResetError(sender, e);
        }

        private void orderGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            orderStatusStrip.Text = "Invalid Order:" + e.Exception.Message;
        }

        private void operationGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            dataGridHelper.Validate(sender, e);
        }

        private void operationGridView_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            dataGridHelper.ResetError(sender, e);
        }


        private void operationGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            operationStatusStrip.Text = "Invalid Operation:" + e.Exception.Message;
        }

        
    }

    public class DataGridHelper
    {
        public void Validate(object sender, DataGridViewCellValidatingEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (dgv == null)
            {
                return;
            }
            if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].IsInEditMode == false)
            {
                return;
            }

            string columnName = dgv.Columns[e.ColumnIndex].DataPropertyName;

            BindingSource bs = dgv.DataSource as BindingSource;
            if (bs == null)
            {
                return;
            }
            DataRowView drv = bs.Current as DataRowView;
            if (drv == null)
            {
                return;
            }


            DataTable dt = drv.DataView.Table;
            if (dt == null)
            {
                return;
            }

            if(dt.Columns[columnName].AllowDBNull == false)
            {
                if (String.IsNullOrEmpty(e.FormattedValue as String) == true)
                {
                    dgv.Rows[e.RowIndex].ErrorText = String.Format("Column {0}: {1}",
                        dgv.Columns[e.ColumnIndex].HeaderText, "cannot be null");
                    e.Cancel = true;
                }
            }

            if (dgv.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn)
            {
                return;
            }

            TypeConverter tc = TypeDescriptor.GetConverter(dt.Columns[columnName].DataType);

            try
            {
                tc.ConvertFrom(e.FormattedValue);
            }
            catch (Exception)
            {
                dgv.Rows[e.RowIndex].ErrorText = String.Format("Column {0}: {1}",
                    dgv.Columns[e.ColumnIndex].HeaderText, "invalid data");
                e.Cancel = true;
            }

        }


        public void ResetError(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (dgv == null)
            {
                return;
            }
            dgv.Rows[e.RowIndex].ErrorText = String.Empty;
        }
    }
}
